home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14281 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: ix.netcom.com!news
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template Class Static Members Initialization
  5. Date: 29 Mar 1996 16:10:52 GMT
  6. Organization: Netcom
  7. Message-ID: <4jh22c$scq@dfw-ixnews6.ix.netcom.com>
  8. References: <31596E00.41C67EA6@cenparmi.concordia.ca>
  9. NNTP-Posting-Host: den-co11-03.ix.netcom.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-NETCOM-Date: Fri Mar 29 10:10:52 AM CST 1996
  13. X-Newsreader: WinVN 0.99.7
  14.  
  15. In article <31596E00.41C67EA6@cenparmi.concordia.ca>, didier@cenparmi.concordia.ca says...
  16. >Having troubles with the initialization of static members of a template class.
  17. >(C++ Primer, S.B. Lippman, 2nd edition, reprint June 93, Section 7.5, pp.369-371)
  18. >Any idea if that's due to my compiler or ?!  Would that compile on yours?
  19.  
  20. BC++4.5 has no problem...
  21.  
  22. >#include <iostream.h>
  23. >
  24. >template <class T>
  25. >class Test
  26. >{
  27. >  private:
  28. >    T n;  
  29. >  public:
  30. >    Test( T i ) : n(i) { count++; }
  31. >    static int count;
  32. >};
  33. >
  34. >template <class T>
  35. >  int Test<T>::count = 0;
  36. >
  37. >int main(int argc, char **argv)
  38. >{
  39. >  Test<int>    t1(1);
  40. >  Test<double> t2(2.0);
  41. >  
  42. >  cout << Test<int>::count << endl;
  43. >}
  44.  
  45. Try explicit instantiation of the static member (in a cpp file, not the header).
  46.  
  47. int Test<int>::count = 0;
  48. int Test<double>::count = 0;
  49. etc.
  50.  
  51. john lilley
  52.  
  53.